#include <map>

#include "asserts.hpp"
#include "terrain_logic.hpp"
#include "wml_parser.hpp"
#include "wml_node.hpp"
#include "wml_utils.hpp"

namespace game {
namespace {
typedef std::map<std::string, const_base_terrain_ptr> BaseTerrainMap;
BaseTerrainMap cache;
}

const_base_terrain_ptr base_terrain::get(const std::string& id)
{
	if(cache.empty()) {
		wml::node_ptr node = wml::parse_wml_from_file("data/base_terrain.cfg");
		FOREACH_WML_CHILD(terrain_node, node, "terrain") {
			const_base_terrain_ptr terrain(new base_terrain(terrain_node));
			cache[terrain->id()] = terrain;
		}
	}

	ASSERT_LOG(cache.count(id), "Could not find base terrain: '" << id << "'\n");
	return cache[id];
}

base_terrain::base_terrain(wml::const_node_ptr node)
  : id_(node->attr("id")), move_cost_(wml::get_int(node, "move", 1)), cover_(wml::get_int(node, "cover")), height_(wml::get_int(node, "height"))
{}

}
